{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "乐学偶得版权所有 lexueoude.com 公众号：乐学Fintech"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Whether the prediction for a given algorithm are good or not?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Baseline Models:"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Random Prediction Algorithm:\n",
    "1.Find unique output values from training data\n",
    "2.Randomly select output from the unique output set "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 1, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "from random import seed\n",
    "from random import randrange\n",
    "\n",
    "def Random_Prediction_Algorithm(traning_data,testing_data):\n",
    "    values=[row[-1] for row in traning_data]\n",
    "    unique_values=list(set(values))\n",
    "    Randomly_predicted_data=list()\n",
    "    for row in testing_data:\n",
    "        index=randrange(len(unique_values))\n",
    "        Randomly_predicted_data.append(unique_values[index])\n",
    "    return Randomly_predicted_data\n",
    "\n",
    "seed(888)\n",
    "traning_data=[[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]]\n",
    "testing_data=[[None],[None],[None],[None],[None]]\n",
    "\n",
    "predictions=Random_Prediction_Algorithm(traning_data,testing_data)\n",
    "print(predictions)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ZeroR(Zero Rule) Algorithm\n",
    "9 [0] and 1[1]:\n",
    "1. If we use the Random Prediction Algorithm: ((0.9*0.9)+(0.1*0.1))*100=82%\n",
    "2. If we use ZeroR Algorithm , our prediction is 0 : 9/10=90%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "from random import seed\n",
    "from random import randrange\n",
    "\n",
    "def ZeroR_Algorithm_classification(traning_data,testing_data):\n",
    "    values=[row[-1] for row in traning_data]\n",
    "    highest_count=max(set(values),key=values.count)\n",
    "    ZeroR_prediction=[highest_count for i in range(len(testing_data))]\n",
    "    return ZeroR_prediction\n",
    "\n",
    "seed(888)\n",
    "traning_data=[[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]]\n",
    "testing_data=[[None],[None],[None],[None],[None]]\n",
    "predictions=ZeroR_Algorithm_classification(traning_data,testing_data)\n",
    "print(predictions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Regression: mean!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5]\n"
     ]
    }
   ],
   "source": [
    "from random import seed \n",
    "from random import randrange\n",
    "\n",
    "def ZeroR_Algorithm_regression(traning_data,testing_data):\n",
    "    values=[row[-1] for row in traning_data]\n",
    "    prediction=sum(values)/float(len(values))\n",
    "    ZeroR_prediction=[prediction for i in range(len(testing_data))]\n",
    "    return ZeroR_prediction\n",
    "\n",
    "seed(888)\n",
    "traning_data=[[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]\n",
    "testing_data=[[None],[None],[None],[None],[None],[None],[None],[None],[None],[None]]\n",
    "\n",
    "prediciton=ZeroR_Algorithm_regression(traning_data,testing_data)\n",
    "print(prediciton)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
